package com.seer.auto.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Response<T> {
    private T data;
    private ErrorResp error;
    private Pageable pageable;

    public static <T> Response<T> ok(T data) {
        Response<T> r = new Response<>();
        r.setData(data);
        r.setError(ErrorCode.SUCCESS.with("ok"));
        return r;
    }

    public static <T> Response<T> error(ErrorResp error) {
        Response<T> r = new Response<>();
        r.setError(error);
        return r;
    }

    public <T> Response<T> page(int page, int size, long count) {
        this.pageable = new Pageable();
        this.pageable.setPageNumber(page);
        this.pageable.setPageSize(size);
        this.pageable.setTotal(count);
        this.pageable.setTotalPages(Math.max((int) Math.ceil((double)(count / size)), 1));
        return (Response<T>) this;
    }
}